package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.exception.CodeMsg;
import cn.wolfcode.shop.exception.GlobalException;
import cn.wolfcode.shop.vo.JSONResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 统一异常管理
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JSONResult exceptionHandler(Exception e){
        e.printStackTrace();//将错误信息打印到控制台
        JSONResult jsonResult=new JSONResult();
        if(e instanceof GlobalException){
            GlobalException ge = (GlobalException)e;
            CodeMsg codeMsg = ge.getCodeMsg();
            jsonResult.setExceptionMsg(codeMsg);
        }else{
            jsonResult.setExceptionMsg(CodeMsg.SERVER_EXPTION);
        }
        return jsonResult;
    }
}
